package com.xhs.main.controller.scgl;

import com.xhs.main.entity.scgl.*;
import com.xhs.main.service.scgl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class scglTBController {
    @Autowired
    ScglScjhService scglScjhService ;
    @Autowired
    ScglScjhmxService scglScjhmxService;
    @Autowired
    ScglWzxqjhService scglWzxqjhService;
    @Autowired
    ScglScrwService scglScrwService;
    @Autowired
    ScglRehbRyztService scglRehbRyztService;
    @Autowired
    ScglRehbScztService scglRehbScztService;
    @Autowired
    ScglRwhbService scglRwhbService;

    @RequestMapping("/scgl_scjhInsert")//制定生产计划,往数据库添加信息
    public String scgl_scjhcontroller01(HttpServletRequest req, HttpServletResponse Res){

        HttpSession session=req.getSession();
        String   scgl_xuanzhe  =String.valueOf( session.getAttribute("scgl_xuanzhe"));
        String scgl_scjh_documentNum = req.getParameter("scgl_scjh_documentNum");
        String scgl_scjh_subject = req.getParameter("scgl_scjh_subject");
        String scgl_scjh_personInCharge = req.getParameter("scgl_scjh_personInCharge");
        String scgl_scjh_department = req.getParameter("scgl_scjh_department");
        String scgl_scjh_project = req.getParameter("scgl_scjh_project");
        Integer scgl_scjh_allProductionNum =Integer.parseInt(req.getParameter("scgl_scjh_allProductionNum")) ;
        String scgl_scjh_singlePlayer = req.getParameter("scgl_scjh_singlePlayer");
        String scgl_scjh_documentDate = req.getParameter("scgl_scjh_documentDate");
        String scgl_scjh_remarks = req.getParameter("scgl_scjh_remarks");
        ScglScjhmx scglScjhmx= scglScjhmxService.scgl_scglScjhmx_selectOne(scgl_xuanzhe);
        scglScjhmxService.scgl_scglScjhmx_update(scgl_xuanzhe,scgl_scjh_documentNum);
        ScglScjh scglScjh=new ScglScjh(null,scgl_scjh_documentNum,scgl_scjh_subject,scgl_scjh_personInCharge
                ,scgl_scjh_department,scgl_scjh_project,scgl_scjh_allProductionNum,1,
                scgl_scjh_singlePlayer,null,scgl_scjh_documentDate,null,null,null,null,1,
                null,scgl_scjh_remarks,scglScjhmx.getScglScjhmxId(),null,null,0.0);
        if(scglScjh!=null){
            scglScjhService.scgl_scglScjh_insert(scglScjh);
        }
        return "redirect:/#/page/scgl/scgl_scjhList.html";
    }

    @RequestMapping("/scgl_scjhupdate")//审批完成,修改数据库中的确认人,确认时间,审批状态,执行状态
    public String scgl_scjhcontroller02(HttpServletRequest req, HttpServletResponse Res){
        String xixi=null;
        String scgl_scjhmx_documentNum = req.getParameter("scgl_scjhmx_documentNum");
        String scgl_scjhmx_confirmPeople = req.getParameter("scgl_scjhmx_confirmPeople");
        String scgl_scjhmx_confirmDate = req.getParameter("scgl_scjhmx_confirmDate");
        String scgl_scjhmx_remarks = req.getParameter("scgl_scjhmx_remarks");
        if(scgl_scjhmx_remarks==null||scgl_scjhmx_remarks==""){
            xixi="";
        }else {
            xixi=scgl_scjhmx_remarks;
        }
        scglScjhService.scgl_scglScjh_updateStatus(scgl_scjhmx_confirmPeople,scgl_scjhmx_confirmDate,2,2,scgl_scjhmx_documentNum,xixi);
        return "redirect:/#/page/scgl/scgl_scjhList.html";
    }
//生产计划订单============================================================================================

    @RequestMapping("/scgl_scjhmxInsert")//制定生产订单,往数据库添加信息
    public String scgl_scjhcontroller03(HttpServletRequest req, HttpServletResponse Res){
        Integer scgl_scjhmx_demandNum =0;
        String scgl_scjhmx_itemNum = req.getParameter("scgl_scjhmx_itemNum");
        String scgl_scjhmx_itemName = req.getParameter("scgl_scjhmx_itemName");
        String scgl_scjhmx_specifications = req.getParameter("scgl_scjhmx_specifications");
        String scgl_scjhmx_unit = req.getParameter("scgl_scjhmx_unit");
        if(req.getParameter("scgl_scjhmx_demandNum")!=null||req.getParameter("scgl_scjhmx_demandNum")!=""){
        scgl_scjhmx_demandNum = Integer.parseInt(req.getParameter("scgl_scjhmx_demandNum"));
        }
        String scgl_scjhmx_startDate = req.getParameter("scgl_scjhmx_startDate");
        String scgl_scjhmx_overDate = req.getParameter("scgl_scjhmx_overDate");
        ScglScjhmx scglScjhmx=new ScglScjhmx(null,scgl_scjhmx_itemNum,scgl_scjhmx_itemName,scgl_scjhmx_specifications,
                scgl_scjhmx_unit,scgl_scjhmx_demandNum,scgl_scjhmx_startDate,scgl_scjhmx_overDate,null);
        scglScjhmxService.scgl_scglScjhmx_insert(scglScjhmx);
        return "redirect:/#/page/scgl/scgl_scjh_oeder.html";
    }

//物资需求计划=============================================================================================
@RequestMapping("/scgl_wzxqjhInsert")//制定物资需求计划,往数据库添加信息
public String scgl_scjhcontroller04(HttpServletRequest req, HttpServletResponse Res){
    String scgl_wzxqjh_mpp_id = req.getParameter("scgl_myselect_id");
    String scgl_wzxqjh_documentNum = req.getParameter("scgl_wzxqjh_documentNum");
    String scgl_wzxqjh_subject = req.getParameter("scgl_wzxqjh_subject");
    String scgl_wzxqjh_personInCharge = req.getParameter("scgl_wzxqjh_personInCharge");
    String scgl_wzxqjh_department = req.getParameter("scgl_wzxqjh_department");
    Integer scgl_wzxqjh_allProductionNum =Integer.parseInt(req.getParameter("scgl_wzxqjh_allProductionNum"));
    String scgl_wzxqjh_singlePlayer = req.getParameter("scgl_wzxqjh_singlePlayer");
    String scgl_wzxqjh_documentDate = req.getParameter("scgl_wzxqjh_documentDate");
    ScglWzxqjh scglWzxqjh=new ScglWzxqjh(null,scgl_wzxqjh_documentNum,scgl_wzxqjh_subject,scgl_wzxqjh_personInCharge,
            scgl_wzxqjh_department,scgl_wzxqjh_mpp_id,1,scgl_wzxqjh_singlePlayer,null,scgl_wzxqjh_documentDate,null,null,
            null,null,1,null,null,scgl_wzxqjh_allProductionNum);
    scglWzxqjhService.scgl_scglWzxqjh_insert(scglWzxqjh);
    scglScjhService.update_wzxqjh_id(scglWzxqjhService.scgl_scglWzxqjh_selectOne(scgl_wzxqjh_documentNum).getScglWzxqjhId(),scgl_wzxqjh_mpp_id);

     return "redirect:/#/page/scgl/scgl_wzxqjhList.html";
}

    @RequestMapping("/scgl_wzcqjhupdate")//审批完成,修改数据库中的确认人,确认时间,审批状态,执行状态
    public String scgl_scjhcontroller05(HttpServletRequest req, HttpServletResponse Res){
        String xixi=null;
        String scgl_scjhmx_documentNum = req.getParameter("scgl_scjhmx_documentNum");
        String scgl_scjhmx_confirmPeople = req.getParameter("scgl_scjhmx_confirmPeople");
        String scgl_scjhmx_confirmDate = req.getParameter("scgl_scjhmx_confirmDate");
        String scgl_scjhmx_remarks = req.getParameter("scgl_scjhmx_remarks");
        if(scgl_scjhmx_remarks==null||scgl_scjhmx_remarks==""){
            xixi="";
        }else {
            xixi=scgl_scjhmx_remarks;
        }
       scglWzxqjhService.scgl_scglWzxqjh_updateStatus(scgl_scjhmx_confirmPeople,scgl_scjhmx_confirmDate,2,2,scgl_scjhmx_documentNum,xixi);
        return "redirect:/#/page/scgl/scgl_wzxqjhList.html";
    }

//任务计划============================================================================================================
@RequestMapping("/scgl_scrwjhjInsert")//制定物资需求计划,往数据库添加信息
public String scgl_scjhcontroller06(HttpServletRequest req, HttpServletResponse Res){
    String scgl_scrw_documentNum = req.getParameter("scgl_scrw_documentNum");
    String scgl_scrw_subject = req.getParameter("scgl_scrw_subject");
    String scgl_scrw_scbm = req.getParameter("scgl_scrw_scbm");
    String scgl_scrw_machiningType = req.getParameter("scgl_scrw_machiningType");
    String scgl_scrw_personInCharge = req.getParameter("scgl_scrw_personInCharge");
    Integer scgl_scrw_currentPlanNum =Integer.parseInt(req.getParameter("scgl_scrw_currentPlanNum"));
    String scgl_scrw_belongingProject = req.getParameter("scgl_scrw_belongingProject");
    String scgl_scrw_singlePlayer = req.getParameter("scgl_scrw_singlePlayer");
    String scgl_scrw_documentDate = req.getParameter("scgl_scrw_documentDate");
    String scgl_scrw_scjh_documentNum = req.getParameter("scgl_scrw_scjh_documentNum");
    String scgl_scrw_remarks = req.getParameter("scgl_scrw_remarks");
    ScglScrw scglScrw=new ScglScrw(
      null,scgl_scrw_scjh_documentNum,scgl_scrw_documentNum,scgl_scrw_subject,scgl_scrw_scbm,scgl_scrw_machiningType,
      scgl_scrw_personInCharge,scgl_scrw_belongingProject,scgl_scrw_currentPlanNum,1,scgl_scrw_singlePlayer,scgl_scrw_documentDate,
      null,null,null,null,1,null,scgl_scrw_remarks,null
    );
    scglScrwService.scgl_scglScrw_insert(scglScrw);
    scglScjhService.update_rwjh_id(scglScrwService.scgl_scglScrw_selectOne(scgl_scrw_documentNum).getScglScrwId(),scgl_scrw_scjh_documentNum);

    return "redirect:/#/page/scgl/scgl_scrwList.html";
}

    @RequestMapping("/scgl_scrwjhupdate")//审批完成,修改数据库中的确认人,确认时间,审批状态,执行状态
    public String scgl_scjhcontroller07(HttpServletRequest req, HttpServletResponse Res){
        String xixi=null;
        String scgl_scjhmx_documentNum = req.getParameter("scgl_scjhmx_documentNum");
        String scgl_scjhmx_confirmPeople = req.getParameter("scgl_scrw_singlePlayer");
        String scgl_scjhmx_confirmDate = req.getParameter("scgl_scjhmx_confirmDate");
        String scgl_scjhmx_remarks = req.getParameter("scgl_scjhmx_remarks");
        if(scgl_scjhmx_remarks==null||scgl_scjhmx_remarks==""){
            xixi="";
        }else {
            xixi=scgl_scjhmx_remarks;
        }
        scglScrwService.scgl_scglScrw_updateStatus(scgl_scjhmx_confirmPeople,scgl_scjhmx_confirmDate,2,2,scgl_scjhmx_documentNum,xixi);
        return "redirect:/#/page/scgl/scgl_scrwList.html";
    }

//任务汇报============================================================================================================

    @RequestMapping("/scgl_rwhbjInsert")//制定物资需求计划,往数据库添加信息
    public String scgl_scjhcontroller08(HttpServletRequest req, HttpServletResponse Res){
        String scgl_scrw_scjh_documentNum = req.getParameter("scgl_scrw_scjh_documentNum");
        String scgl_scrw_subject = req.getParameter("scgl_scrw_subject");
        String scgl_scrw_documentNum = req.getParameter("scgl_scrw_documentNum");
        String scgl_rwhb_singlePlayer = req.getParameter("scgl_rwhb_singlePlayer");
        String scgl_rwhb_confirmDate = req.getParameter("scgl_rwhb_confirmDate");
        Integer scgl_rehb_sczt_itemName =Integer.parseInt(req.getParameter("scgl_rehb_sczt_itemName"));//存进生产状态中的计划数
        String scgl_rehb_sczt_itemNum = req.getParameter("scgl_rehb_sczt_itemNum");//存进生产状态中的源单编号
        Double scgl_rehb_sczt_completeNum = Double.parseDouble(req.getParameter("scgl_rehb_sczt_completeNum"));//存进生产状态中的完成数
        Double scgl_rehb_sczt_qualified = Double.parseDouble(req.getParameter("scgl_rehb_sczt_qualified"));//存进生产状态中的合格数
        Double scgl_rehb_sczt_workingHour = Double.parseDouble(req.getParameter("scgl_rehb_sczt_workingHour"));//存进生产状态中的工时
        String scgl_scrw_remarks = req.getParameter("scgl_scrw_remarks");
        Integer scgl_rehb_ryzt_attendeesPeople =Integer.parseInt(req.getParameter("scgl_rehb_ryzt_attendeesPeople"));
        Integer scgl_rehb_ryzt_actualNum =Integer.parseInt(req.getParameter("scgl_rehb_ryzt_actualNum"));
        Double scgl_rehb_ryzt_attendeesWorkHoure =Double.parseDouble(req.getParameter("scgl_rehb_ryzt_attendeesWorkHoure"));
        Double scgl_rehb_ryzt_workOvertime =Double.parseDouble(req.getParameter("scgl_rehb_ryzt_workOvertime"));
        Double scgl_rehb_ryzt_shutDownTime =Double.parseDouble(req.getParameter("scgl_rehb_ryzt_shutDownTime"));
        ScglRwhb scglRwhb=new ScglRwhb(null,scgl_scrw_documentNum,scgl_scrw_subject,scgl_scrw_scjh_documentNum,scgl_rwhb_singlePlayer,scgl_rwhb_confirmDate);
        scglRwhbService.scgl_scglRwhb_insert(scglRwhb);//添加生产汇报
        String Qualifiedrate=String.format("%.2f", scgl_rehb_sczt_qualified/scgl_rehb_sczt_completeNum);

        double scglRehbScztQualifiedrate=Double.parseDouble(Qualifiedrate);
        ScglRehbSczt scglRehbSczt=new ScglRehbSczt(null,scgl_rehb_sczt_completeNum,
                scglRwhbService.scgl_scglRwhb_selectOne(scgl_scrw_documentNum).getScglRwhbId(),
                scgl_rehb_sczt_itemNum,scgl_rehb_sczt_itemName,scgl_rehb_sczt_workingHour,scgl_rehb_sczt_qualified,Qualifiedrate);
        scglRehbScztService.scgl_ScglRehbSczt_insert(scglRehbSczt);
        ScglRehbRyzt scglRehbRyzt=new ScglRehbRyzt(
null,scgl_rehb_ryzt_attendeesPeople,scglRwhbService.scgl_scglRwhb_selectOne(scgl_scrw_documentNum).getScglRwhbId(),
                scgl_rehb_ryzt_actualNum,scgl_rehb_ryzt_attendeesWorkHoure,scgl_rehb_ryzt_workOvertime,scgl_rehb_ryzt_shutDownTime
        );
        scglRehbRyztService.scgl_rehbRyztDao_insert(scglRehbRyzt);
        Double thisWangchen=null;
        ScglScrw scglScrw = scglScrwService.scgl_scglScrw_selectOne(scgl_scrw_scjh_documentNum);
        ScglScjh scglScjh = scglScjhService.scgl_scglScjh_selectOne(scglScrw.getScglScrwScjhDocumentnum());
       if(scglScjh.getScglScjhCompleteNum()==null){
           thisWangchen=scgl_rehb_sczt_qualified;
       }else{
           thisWangchen=scglScjh.getScglScjhCompleteNum()+scgl_rehb_sczt_qualified;
       }
        scglScrwService.scgl_scglScrw_updateStatus(scglScrw.getScglScrwConfirmpeople(),scglScrw.getScglScrwConfirmdate(),3,2,scglScrw.getScglScrwDocumentnum(),scglScrw.getScglScrwRemarks());
        scglScjhService.update_scgl_scjh_completeNum(thisWangchen,scglScrw.getScglScrwScjhDocumentnum());
        ScglWzxqjh scglWzxqjh = scglWzxqjhService.scgl_scglWzxqjh_selectOne_byshengchanjihua(scglScrw.getScglScrwScjhDocumentnum());


        scglScrwService.scgl_scglScrw_updateLast(scgl_rwhb_singlePlayer,scgl_rwhb_confirmDate,scgl_rwhb_singlePlayer,scgl_rwhb_confirmDate,scglScrw.getScglScrwDocumentnum());
        if(thisWangchen>=scglScjh.getScglScjhAllproductionnum()){
            scglScjhService.scgl_scglScjh_updateLast(scgl_rwhb_singlePlayer,scgl_rwhb_confirmDate,scgl_rwhb_singlePlayer,scgl_rwhb_confirmDate,scglScrw.getScglScrwScjhDocumentnum());
            scglScjhService.scgl_scglScjh_updateStatus(scglScjh.getScglScjhConfirmpeople(),scglScjh.getScglScjhConfirmdate(),3,2,scglScrw.getScglScrwScjhDocumentnum(),scglScjh.getScglScjhRemarks());
            scglWzxqjhService.scgl_scglWzxqjh_updateStatus(scglWzxqjh.getScglWzxqjhConfirmpeople(),scglWzxqjh.getScglWzxqjhConfirmdate(),3,2,scglWzxqjh.getScglWzxqjhDocumentnum(),scglWzxqjh.getScglWzxqjhRemarks());
            scglWzxqjhService.scgl_scglWzxqjh_updateLast(scgl_rwhb_singlePlayer,scgl_rwhb_confirmDate,scgl_rwhb_singlePlayer,scgl_rwhb_confirmDate,scglScrw.getScglScrwScjhDocumentnum());
        }


        return "redirect:/#/page/scgl/scgl_rwhbList.html";
    }









}

